package wsclients;

import java.io.IOException;

import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
import org.codehaus.xfire.MessageContext;
import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.handler.AbstractHandler;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import org.codehaus.xfire.transport.http.EasySSLProtocolSocketFactory;
import org.codehaus.xfire.util.LoggingHandler;
import org.codehaus.xfire.util.dom.DOMInHandler;
import org.codehaus.xfire.util.dom.DOMOutHandler;
import org.jdom.Element;
import org.jdom.Namespace;

import com.avetti.commerce.paymentserver.webservices.PaymentServerService.PaymentServerService;

public class PaymentServerServiceTest {

    static {
        ProtocolSocketFactory easy = new EasySSLProtocolSocketFactory();
        Protocol protocol = new Protocol("https", easy, 443);
        Protocol.registerProtocol("https", protocol);
    }

    public void test() throws IOException {
        String url="http://localhost/pcisecure/services/paymentserverservice";
        String vid="20070723002";
        String mid="avetti";
        String user="commusr";
        String pwd="Avetti.com1234";
        
        Service serviceModel = new ObjectServiceFactory().create(PaymentServerService.class);
        PaymentServerService service = (PaymentServerService) new XFireProxyFactory().create(
                serviceModel, url);
        
        serviceModel.addInHandler(new DOMInHandler());
        serviceModel.addOutHandler(new DOMOutHandler());
        serviceModel.addFaultHandler(new DOMOutHandler());
        
        serviceModel.addInHandler(new LoggingHandler());
        serviceModel.addOutHandler(new LoggingHandler());
        serviceModel.addFaultHandler(new LoggingHandler());

        Client client = Client.getInstance(service);
        client.addOutHandler(new VendorIdHandler(vid));
        client.addOutHandler(new PCIUserPassHandler(mid,user, pwd));
        
        Object obj = service.test(1);
        System.out.println(obj.getClass().getName());
        
        
        obj = service.test(2);
        System.out.println(obj.getClass().getName());
    }

    private class VendorIdHandler extends AbstractHandler {
        private String vendorId;

        VendorIdHandler(String vendorId) {
            super();
            this.vendorId = vendorId;
        }

        public String getVendorId() {
            return vendorId;
        }

        public void setVendorId(String vendorId) {
            this.vendorId = vendorId;
        }

        public void invoke(MessageContext context) throws Exception {
            final Namespace ns = Namespace.getNamespace("urn:commerce:vendor");
            Element header = context.getOutMessage().getHeader();

            if (header == null) {
                header = new Element("header");
                context.getOutMessage().setHeader(header);
            }

            Element vendor = new Element("VendorToken", ns);
            Element vendor_el = new Element("VendorId", ns);
            vendor_el.addContent(vendorId);
            vendor.addContent(vendor_el);
            header.addContent(vendor);
        }
    }

    private class PCIUserPassHandler extends AbstractHandler {
        private String mid;
        private String user;
        private String pass;

        PCIUserPassHandler(String mid, String user, String pass) {
            super();
            this.mid = mid;
            this.user = user;
            this.pass = pass;
        }

        public void invoke(MessageContext context) throws Exception {
            final Namespace ns = Namespace.getNamespace("urn:commerce:authentication");
            Element header = context.getOutMessage().getHeader();

            if (header == null) {
                header = new Element("header");
                context.getOutMessage().setHeader(header);
            }

            Element root = new Element("AuthenticationToken", ns);
            Element mid_el = new Element("Merchant", ns);
            Element username_el = new Element("Username", ns);
            Element password_el = new Element("Password", ns);

            mid_el.addContent(mid);
            username_el.addContent(user);
            password_el.addContent(pass);
            
            root.addContent(mid_el);
            root.addContent(username_el);
            root.addContent(password_el);

            header.addContent(root);
        }
    }

    public static void main(String[] args) throws IOException {
        PaymentServerServiceTest cst=new PaymentServerServiceTest();
        cst.test();
    }
}
